#pragma once
#include "Util.hpp"
#include <iostream>
#include <vector>
#include <jsoncpp/json/json.h>
#include <ctemplate/template.h>
class Oj_View{
    public:
      Oj_View() = default;
      static void RenderAllQuestions(const std::vector<Json::Value>& v, std::string& html) {
        ctemplate::TemplateDictionary dict("problemD"); 
        for (const auto& i: v) {
            ctemplate::TemplateDictionary* item; 
            item = dict.AddSectionDictionary("question");
            item -> SetValue("id", i["ProblemID"].asString());
            item -> SetValue("name", i["ProblemName"].asString());
            item -> SetValue("star", i["Difficulty"].asString());
        }
        std::string output;
        ctemplate::Template* tpl;
        /*现在主页不需要动态渲染*/
        tpl = ctemplate::Template::GetTemplate("./Web/problemD.html", ctemplate::DO_NOT_STRIP);
        tpl -> Expand(&output, &dict);
        /*将html通过引用返回*/
        html = output;
        LOG(INFO, html);
      } 
      static void RenderOneQuestion(const Json::Value& obj, std::string& html) {

        ctemplate::TemplateDictionary dict("submitPageD"); 
        dict.SetValue("Name", obj["ProblemName"].asString());
        dict.SetValue("Description", obj["Description"].asString());
        dict.SetValue("id", obj["ProblemID"].asString());
        std::string output;
        ctemplate::Template* tpl;
        /*现在主页不需要动态渲染*/
        tpl = ctemplate::Template::GetTemplate("./Web/submitPageD.html", ctemplate::DO_NOT_STRIP);
        tpl -> Expand(&output, &dict);
        /*将html通过引用返回*/
        html = output;
        LOG(INFO, html);
      } 
      static void RenderSubmitStatus(const Json::Value& obj, std::string& html) {

        ctemplate::TemplateDictionary dict("compileD"); 
        dict.SetValue("ProblemName", obj["ProblemName"].asString());
        dict.SetValue("Status", obj["status"].asString()); 
        const auto status = obj["status"].asString();
        if (status == "Hack Code") {
            dict.SetValue("error", obj["reason"].asString());
        }
        if (status == "Compile Error") {
            dict.SetValue("error", obj["stderr"].asString());
        }
        if (status == "Run Error") {
            dict.SetValue("error", obj["stdout"].asString());
        }
        if (status == "Wrong Answer" || status == "Accept") {
            dict.SetValue("error", obj["totaltime"].asString() + "\n" + obj["totalmemory"].asString());
        } 
        if (status == "Time Limit Exceeded ") {
            dict.SetValue("error", "Time Limit is 5 seconds");
        }
        const auto testnum = obj["TestNum"].asInt();
        for (int i = 1; i <= testnum; i++) {
            ctemplate::TemplateDictionary* item; 
            item = dict.AddSectionDictionary("Test");
            item -> SetValue("TestName", "test" + std::to_string(i));
            item -> SetValue("TestStatus", obj["test" + std::to_string(i)].asString());
            item -> SetValue("TestCostTime", obj["time" + std::to_string(i)].asString() + "MS");
            item -> SetValue("TestCostMemory", obj["memory" + std::to_string(i)].asString() + "KB");
        }
        std::string output;
        ctemplate::Template* tpl;
        tpl = ctemplate::Template::GetTemplate("./Web/compileD.html", ctemplate::DO_NOT_STRIP);
        tpl -> Expand(&output, &dict);
        html = output;
        LOG(INFO, html);
      }
      static void RenderCode(const Json::Value& obj, std::string& html) {
        ctemplate::TemplateDictionary dict("CodeLookD"); 
        dict.SetValue("code", obj["code"].asString());
        std::string output;
        ctemplate::Template* tpl;
        tpl = ctemplate::Template::GetTemplate("./Web/CodeLookD.html", ctemplate::DO_NOT_STRIP);
        tpl -> Expand(&output, &dict);
        html = output;
        LOG(INFO, html);
      }
    private:

};
